<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');


/**
 * Initialize Messages that will be shown on the module screen.
 * @access public 
 */
function init_module()
{
    $ci = &get_instance();
    
    // initialize module messages
    init_messages('Module');
    
//    // initialize table query table columns
//    $ci->alias = 'id, name, description, date_created, date_modified, status';
//    
//    // initialize more columns to be shown
//    $ci->more_columns = 'id, name, description, date_created, date_modified, status';
//    
//    // initialize search columns
//    $ci->search_columns = array('name', 'description', 'status');
//    
//    // initialize table headers
//    $table_headers = array();
//    $table_headers['name'] = 'Name';
//    $table_headers['description'] = 'Description';
//    $table_headers['date_created'] = 'Date Created';
//    $table_headers['date_modified'] = 'Date Modified';
//    $table_headers['status'] = 'Status';
//    $ci->table_headers = create_datatable_headers($table_headers, 'modules', 2);
//
//    // initialize customized table columns
//    $ci->custom_columns = array();
//    
//    $date_created = array();
//    $date_created['keys'] = 'cms_date(date_created)';
//    $date_created['markup'] = '$1';
//    $ci->custom_columns['date_created'] = $date_created;
//    
//    $date_modified = array();
//    $date_modified['keys'] = 'cms_date(date_modified)';
//    $date_modified['markup'] = '$1';
//    $ci->custom_columns['date_modified'] = $date_modified;
//    
//    $status['keys'] = 'cms_status_toggle(status, modules/confirm_update/, id)';
//    $status['markup'] = '$1';
//    $ci->custom_columns['status'] = $status;
}

/*
 * Method for validating posted values.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise sends invalid response.
 */
function valid_add_module()
{
    $ci = &get_instance();
    $cms = $ci->cms;

    $ci->form_validation
            ->set_rules('name',         'name',         'trim|required|max_length[20]|xss_clean')
            ->set_rules('description',  'description',  'trim|required|max_length[1000]|xss_clean')
            ->set_rules('permission[]', 'permissions',  'trim|required|xss_clean')
            ->set_rules('status',       'status',       'trim|max_length[10]|xss_clean');
            
    return $ci->form_validation->run();
}

/*
 * Method for validating posted values.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise sends invalid response.
 */
function valid_edit_module()
{
    $ci = &get_instance();
    
    $ci->input->post('id')           ? $ci->form_validation->set_rules('id',             'ID',           'trim|required|numeric|max_length[10]|xss_clean')   : '';
    $ci->input->post('name')         ? $ci->form_validation->set_rules('name',           'name',         'trim|required|max_length[20]|xss_clean')           : '';
    $ci->input->post('description')  ? $ci->form_validation->set_rules('description',    'description',  'trim|required|max_length[1000]|xss_clean')         : '';
    $ci->input->post('permission')   ? $ci->form_validation->set_rules('permission[]',   'permission',   'trim|required|xss_clean')                          : '';
    $ci->input->post('status')       ? $ci->form_validation->set_rules('status',         'status',       'trim|max_length[10]|xss_clean')                    : '';
    
    return $ci->form_validation->run();
}

/**
 * Displays modules list page.
 * @access public
 * @param array $content An array of modules to be shown.
 * @param string $message The type of message to be shown.
 */
function view_modules_list($content = NULL, $message_type = NULL)
{
    $ci = &get_instance();

    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Modules');
    
    $data = array();
    
    // set message
    $message                    = get_message($message_type);
    $data['message_type']       = $message['message_type'];
    $data['message']            = $message['message'];
    $data['message_title']      = $message['title'];
    $data['message_class']      = $message['class'];
    
    if($content === NULL)
    {
        $data['modules'] = $ci->module_model->get_all();
    } else
    {
        $data['modules'] = $content;
    }

    // active navigation
    $data['navigation'] = 'modules';

    // build breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Modules');

    // build template
    $ci->template
            ->title('Modules')
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('module_list', $data);
}

/**
 * Displays module info.
 * @access public
 * @param array $content An array of record info.
 */
function view_module_preview($content = NULL)
{
    $ci = &get_instance();

    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Preview Module');
    
    $data = array();

    if($content === NULL)
    {
        $data['modules'] = $ci->module_model->get_all();
    } else
    {
        $data['modules'] = $content;
    }

    // active navigation
    $data['navigation'] = 'modules';

    // build breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Modules', base_url() . 'modules');
    $ci->breadcrumb->add_crumb('Module Preview');
    
    // build template
    $ci->template
            ->title('Module')
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
//            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('module_preview', $data);
}

/**
 * Displays the form for adding or editing modules.
 * @access public
 * @param string $title String representing the title page.
 * @param string $crumb The string that will be appended to the end of the breadcrumb.
 * @param string $message The type of message that will be shown on the form. Default value is null.
 * @param array $record An array containing record info to be edited. Default value is null.
 */
function view_module_form($title, $message_type = NULL, $record = NULL)
{
    $ci = &get_instance();
    $data = array();
    
    // set message
    $message                    = get_message($message_type);
    $data['message_type']       = $message['message_type'];
    $data['message']            = $message['message'];
    $data['message_title']      = $message['title'];
    $data['message_class']      = $message['class'];

    // set form data
    $data['action'] = $title == 'Add Module' ? 'add' : 'edit';
    $data['add'] = $title == 'Add Module' ? TRUE : FALSE;
    $data['add_another'] = $ci->input->post('add-another') ? ' checked="checked"' : '';

    // set reservation data
    $data['id'] = isset($record['id']) ? $record['id'] : set_value('id');
    
    $data['name'] = isset($record['name']) ? $record['name'] : set_value('name');
    $data['name-status'] = form_error('name') ? ' has-error' : '';
    
    $data['description'] = isset($record['description']) ? $record['description'] : set_value('description');
    $data['description-status'] = form_error('description') ? ' has-error' : '';
    
    $data['status'] = isset($record['status']) ? $record['status'] : set_value('status');
    
    // set active navigation
    $data['navigation'] = 'modules';
    
    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Modules', base_url() . 'modules');
    $ci->breadcrumb->add_crumb($title);

    // build template
    $ci->template
            ->title($title)
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('module_form', $data);
    
}

/**
 * Formats an array of records to valid response.
 * @access public
 * @param array $modules Array of modules
 */
function response_modules($modules)
{
    $ci = &get_instance();
    
    $params = $ci->input->post();
    $case = isset( $params['case'] ) ? $params['case'] : 'snake';
    
    $response_modules = array();
    foreach($modules as $module)
    {
        $response_module                   = array();
        isset($module['id'])               ? $response_module[changeCase('id', $case)]               = clean( $module['id'] ) : '';
        isset($module['name'])             ? $response_module[changeCase('name', $case)]             = clean( $module['name'] ) : '';
        isset($module['description'])      ? $response_module[changeCase('description', $case)]      = clean( $module['description'] ) : '';
        isset($module['permissions'])      ? $response_module[changeCase('permissions', $case)]      = clean( $module['permissions'] ) : '';
        isset($module['date_created'])     ? $response_module[changeCase('date_created', $case)]     = clean( $module['date_created'] ) : '';
        isset($module['date_modified'])    ? $response_module[changeCase('date_modified', $case)]    = clean( $module['date_modified'] ) : '';
        isset($module['status'])           ? $response_module[changeCase('status', $case)]           = clean( $module['status'] ) : '';

        array_push($response_modules, $response_module);
    }
    
    // we format our response
    if( count($response_modules) > 0 ) // check if not empty
    {
        format_response('success', 200, 'Response Modules.', NULL, $response_modules);
    }
    else
    {
        format_response('success', 200, 'No Data Available', NULL, 'No Data Available');
    }
    
}


/* End of file module_helper.php */
/* Location: ./application/modules/cms-modules/helpers/module_helper.php */